package com.example.appinting.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.appinting.Adapter.DoctorAdapter;
import com.example.appinting.Beans.doctor;
import com.example.appinting.R;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Index extends Fragment {
    private View view;
    private ListView listView;
    private Button btn_search;

    private DoctorAdapter adapter;
    private List<HashMap<String, Object>> list;

    private List<doctor> doctors;
    private String username;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //list必须初始化不然会闪退
        list = new ArrayList<>();

        Bundle bundle = getArguments();
        if (bundle != null && bundle.containsKey("doctorList")) {

            doctors = (List<doctor>) bundle.getSerializable("doctorList");
            System.out.println("bundle不为空");
//            System.out.println(doctors.get(0).getDoctor_name());
            //初始化数据
            initDate();
        }
        else {
            System.out.println("bundle为空");
        }

        adapter = new DoctorAdapter(getActivity(), list);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//        username = getActivity().getIntent().getStringExtra("name");
        view = inflater.inflate(R.layout.doctor, container, false);
        listView = (ListView) view.findViewById(R.id.listview);



////        // 3 填充
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return view;
    }

    private void initDate() {
//        System.out.println(doctors.get(0).getDoctor_name());
        try {
            for (doctor doctor :doctors){
                HashMap<String, Object> map = new HashMap<>();
                map.put("Doctor_id", doctor.getDoctor_id());
                map.put("Doctor_name", doctor.getDoctor_name());
                map.put("Phone_number", doctor.getPhone_number());
                map.put("Department",doctor.getDepartment());
                list.add(map);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
